/**
 * Copyright (c) 2017-2017 All Rights Reserved.
 */
package com.zbest.zpay.exchange.biz.action.pay;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zbest.zpay.exchange.dal.dao.FluxInfoDAO;
import com.zbest.zpay.exchange.dal.model.FluxInfoDO;
import com.zbest.zpay.exchange.helper.StatusManager;
import com.zbest.zpay.service.exchange.model.FluxInfoStatus;
import com.zbest.zpay.service.exchange.model.FluxKeyEnum;
import com.zbest.zpay.service.gw.model.GwResult;
import com.zbest.zpay.service.gw.model.InstStatus;
import com.zbest.zpay.shared.process.BaseAction;
import com.zbest.zpay.shared.process.ProcessResult;

/**
 * 
 *
 * @author Administrator
 * @version $Id: HandleGWResultAction.java, v 0.1 2017-4-24 下午11:34:21 Administrator Exp $
 */
@Service("handleWXPrepareResultAction")
public class HandleWXPrepareResultAction extends BaseAction {

    @Resource
    private FluxInfoDAO   fluxInfoDAO;

    @Resource
    private StatusManager fluxInfoStatusManager;

    /** 
     * @see com.zbest.zpay.shared.process.BaseAction#perform(com.zbest.zpay.shared.process.ProcessResult)
     */
    @Override
    public void perform(ProcessResult processResponse) {

        GwResult gwResult = getValue(FluxKeyEnum.GW_RESULT.getCode());

        FluxInfoDO fluxInfoDO = getValue(FluxKeyEnum.FLUX_INFO_DO.getCode());

        if (gwResult.isSuccess() && gwResult.getInstStatus().equals(InstStatus.SUCCESS.getCode())) {
            //成功只能代表预处理成功，所以不修改状态
            //FIXME 检查下返回数据和请求是否一致
            return;
        }

        //FIXME 判断是否要更新到FA
        fluxInfoStatusManager.statusCheck(FluxInfoStatus.PROCESS.getCode(),
            FluxInfoStatus.FAIL.getCode());
        fluxInfoDAO.updateResponseAndStatus(fluxInfoDO.getFluxId(), gwResult.getCode().code(),
            gwResult.getCode().desc(), gwResult.getInstRespCode(), gwResult.getInstRespMessage(),
            FluxInfoStatus.FAIL.getCode(), FluxInfoStatus.PROCESS.getCode());
    }
}
